package com.zjw.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zjw.annotation.domain.entity.Menu;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 菜单权限表(Menu)表数据库访问层
 *
 * @author ljzy
 * @since 2023-12-20 11:33:19
 */
@Mapper
public interface MenuMapper extends BaseMapper<Menu> {

    /**
     * 获取指定用户的权限信息
     *
     * @param id 编号
     * @return {@link List}<{@link Menu}>
     */
    @Select("select distinct * from sys_menu m " +
            "left join sys_role_menu rm on m.id = rm.menu_id " +
            "left join sys_user_role ur on rm.role_id = ur.role_id " +
            "where ur.user_id = #{id} && m.menu_type in ('C', 'F') && m.status = '0'")
    public List<Menu> getPermsById(@Param("id") Long id);

    /**
     * 获取用户菜单信息
     *
     * @param id 编号
     * @return {@link List}<{@link Menu}>
     */
    @Select("select distinct m.id, m.parent_id, m.menu_name, m.path, m.component, " +
            "m.visible, m.status, IFNULL(m.perms, '') AS perms, m.is_frame, m.menu_type, " +
            "m.icon, m.order_num, m.create_time " +
            "from sys_menu m " +
            "left join sys_role_menu rm on m.id = rm.menu_id " +
            "left join sys_user_role ur on rm.role_id = ur.role_id " +
            "where ur.user_id = #{id} && m.menu_type in ('C', 'M') && m.status = '0' " +
            "order by m.parent_id, m.order_num ")
    public List<Menu> getTreeMenuById(@Param("id") Long id);

    //    超管获取所有菜单信息
    @Select("select distinct m.id, m.parent_id, m.menu_name, m.path, m.component, " +
            "m.visible, m.status, IFNULL(m.perms, '') AS perms, m.is_frame, m.menu_type, " +
            "m.icon, m.order_num, m.create_time " +
            "from sys_menu m " +
            "left join sys_role_menu rm on m.id = rm.menu_id " +
            "left join sys_user_role ur on rm.role_id = ur.role_id " +
            "where m.menu_type in ('C', 'M')  " +
            "order by m.parent_id, m.order_num")
    public List<Menu> getTreeMenuAdmin();

    @Select("select count(*) from sys_menu m where parent_id = #{id}")
    Integer hasSub(@Param("id") Long id);


    /**
     * 删除菜单时，删除角色和菜单的关系
     *
     * @param id 编号
     */
    @Delete("delete from sys_role_menu where menu_id = #{id}")
    void removeFromRoleMenu(@Param("id") Long id);

    @Select("select * from sys_menu where parent_id = #{id}")
    List<Menu> getSubBtn(@Param("id") Long id);

    @Insert("insert into sys_role_menu(role_id, menu_id) values (#{id}, #{mId})")
    void assignAuth(@Param("id") Long id,@Param("mId") Long mId);

    /**
     * 获取指定id角色的所有权限
     *
     * @param id 编号
     * @return {@link List}<{@link Menu}>
     */
    @Select("select * from sys_menu m left join sys_role_menu rm on rm.menu_id = m.id " +
            "where rm.role_id = #{id}")
    List<Menu> getRoleTreeById(@Param("id") Long id);

    /**
     * 删除角色时，移除关系表中角色和菜单关系
     *
     * @param id 编号
     */
    @Delete("delete from sys_role_menu where role_id = #{id}")
    void removeRoleMenuById(@Param("id") Long id);
}
